package com.htc.dlc.fragment;

import java.util.ArrayList;

import com.htc.adapter.WellnessSchAdapter;
import com.htc.dlc.R;
import com.htc.dlc.SettingsActivity;
import com.htc.dlc.WellnessSchDetailsActivity;
import com.htc.model.dto.WellnessSchChildDTO;
import com.htc.model.dto.WellnessSchParentDTO;
import com.htc.model.utils.FitnessModel;
import com.htc.slidingmenu.library.CirclePageIndicator;
import com.htc.slidingmenu.library.PageIndicator;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupExpandListener;

public class FragmentWellnessSchELV extends Fragment {
	ExpandableListView EXPwellness;
	ArrayList<WellnessSchChildDTO> arrChildrenList;
	WellnessSchAdapter wellSchAdapter;
	static ArrayList<WellnessSchParentDTO> arrParentList;
	View footer;
	View header;
	Handler handler;
	public static final int DELAY = 5000;
	ViewPager mPager;
	PageIndicator mIndicator;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_wellness_schedule,
				container, false);
		EXPwellness = (ExpandableListView) rootView.findViewById(R.id.elv_list);
		footer = getActivity().getLayoutInflater().inflate(
				R.layout.activity_footer, null);
		header = getActivity().getLayoutInflater().inflate(
				R.layout.activity_slishow_fitness, null);
		EXPwellness.addHeaderView(header);
		EXPwellness.addFooterView(footer);
		mPager = (ViewPager) header.findViewById(R.id.pager);
		DisplayMetrics displaymetrics = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		int height = displaymetrics.heightPixels;
		mPager.getLayoutParams().height = height*1/3;
		mIndicator = (CirclePageIndicator) header.findViewById(R.id.indicator);
		return rootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		arrParentList = GetSearchResults();
		wellSchAdapter = new WellnessSchAdapter(getActivity(), 0, arrParentList);

		ImagePagerAdapter adapterimg = new ImagePagerAdapter();
		mPager.setAdapter(adapterimg);

		mIndicator.setViewPager(mPager);

		EXPwellness.setAdapter(wellSchAdapter);

		EXPwellness.setOnGroupExpandListener(new OnGroupExpandListener() {

			@Override
			public void onGroupExpand(int groupPosition) {
				String str1 = arrParentList.get(groupPosition)
						.getWellnessSchDay();
				FitnessModel md = FitnessModel.getInstance();
				arrChildrenList = md.query(str1.toLowerCase(), getActivity());

			}
		});
		EXPwellness.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {

				ArrayList<WellnessSchChildDTO> arrData = ((WellnessSchParentDTO) FragmentWellnessSchELV.arrParentList
						.get(groupPosition)).getSchChild();
				String idFN = ((WellnessSchChildDTO) arrData.get(childPosition))
						.getWellnessSchClassId();
				String title = ((WellnessSchChildDTO) arrData
						.get(childPosition)).getWellnessSchTitle();
				String titlear = ((WellnessSchChildDTO) arrData
						.get(childPosition)).getWellnessSchTitleAr();
				String images = ((WellnessSchChildDTO) arrData
						.get(childPosition)).getWellnessSchImage();

				String description = ((WellnessSchChildDTO) arrData
						.get(childPosition)).getWellnessSchDesc();
				String descriptionar = ((WellnessSchChildDTO) arrData
						.get(childPosition)).getWellnessSchDescAr();
				Bundle data = new Bundle();
				Intent intent = new Intent(getActivity(),
						WellnessSchDetailsActivity.class);
				data.putString("ID", idFN);
				data.putString("image", images);
				SettingsActivity st = new SettingsActivity();
				if (st.flag == 0) {
					data.putString("title", title);
					data.putString("description", description);
				} else if (st.flag == 1) {
					data.putString("title", titlear);
					data.putString("description", descriptionar);
				}

				intent.putExtras(data);
				startActivity(intent);

				return false;
			}
		});
	}

	private ArrayList<WellnessSchParentDTO> GetSearchResults() {

		ArrayList<WellnessSchParentDTO> arrParent = new ArrayList<WellnessSchParentDTO>();
		String arrData[] = { "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY",
				"FRIDAY", "SATURDAY", "SUNDAY" };
		for (int i = 0; i < arrData.length; i++) {
			WellnessSchParentDTO dto = new WellnessSchParentDTO();
			dto.setWellnessSchDay(arrData[i]);
			String str1 = arrData[i];
			FitnessModel md = FitnessModel.getInstance();
			arrChildrenList = md.query(str1.toLowerCase(), getActivity());
			if (arrChildrenList != null) {
				dto.setSchChild(arrChildrenList);
			}
			arrParent.add(dto);
		}

		return arrParent;
	}

	private class ImagePagerAdapter extends PagerAdapter {
		private int[] mImages = new int[] { R.drawable.wellness1,
				R.drawable.wellness2, R.drawable.wellness3 };

		@Override
		public int getCount() {
			return mImages.length;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			Context context = getActivity();
			ImageView imageView = new ImageView(context);
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			imageView.setImageResource(mImages[position]);

			((ViewPager) container).addView(imageView, 0);
			return imageView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((ImageView) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return (view == object);
		}

		/**
		 * Called when the a change in the shown pages has been completed. At
		 * this point you must ensure that all of the pages have actually been
		 * added or removed from the container as appropriate.
		 * 
		 * @param arg0
		 *            The containing View which is displaying this adapter's
		 *            page views.
		 */
		@Override
		public void finishUpdate(ViewGroup arg0) {
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(ViewGroup arg0) {
		}
	}
}
